home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / mpfeel.lha / MPFeel / Plurals / visualise.c < prev   
C/C++ Source or Header  |  1992-03-27  |  6KB  |  369 lines

  1. #define _BSD
  2.  
  3. /*#include <stdio.h>*/
  4.  
  5. /*#include <stdlib.h>*/
  6.  
  7. #define XK_LATIN1
  8. #include <X11/Xlib.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/Xos.h>
  11. /*#include <X11/keysymde.h>*/
  12. #include <X11/bitmaps/icon>
  13.  
  14. #include "mp_eubang.h"
  15.  
  16. #define WNAME "XLights"
  17. #define INAME "XLights"
  18.  
  19. int border;
  20. int panel_width;
  21. int panel_height;
  22.  
  23. int light_width;
  24. int light_height;
  25. int loadlight_x;
  26. int loadlight_y;
  27.  
  28. int width, height;
  29. int x,y;
  30.  
  31. int panel_x( x )
  32. int x;
  33. { return ((x)*(panel_width+2*border)+(2*border)); }
  34.  
  35. int panel_y( y )
  36. int y;
  37. { return ((y)*(panel_height+2*border)+(2*border)); }
  38.  
  39. #define XKillPendingRequests( dpy ) ((dpy)->bufptr = (dpy)->buffer)
  40.  
  41. Display *    display;
  42. Visual *    visual;
  43. int         screen;
  44. Window         win;
  45. GC         gc;
  46. XEvent         xevent;
  47. Pixmap         icon_pixmap;
  48. Font         font;
  49. XFontStruct *    fontinfo;
  50. int         black;
  51. int         white;
  52. int         grey = 22;
  53.  
  54. char **        colnames;
  55. int         ncolors;
  56.  
  57. char *        huenames[] =
  58. {
  59.   "black",
  60.   "red",
  61.   "skyblue",
  62.   "green"
  63. };
  64.  
  65. char *        greynames[] =
  66. {
  67.     "black",
  68.     "black",
  69.     "white",
  70.     "white"
  71. };
  72.  
  73. int colors[ 20 ];
  74.  
  75. /* ------------------------- */
  76. #define IOdebug    printf
  77. #define Delay(x)   sleep(x)
  78. #define OneSec     1
  79.  
  80. typedef int word;
  81. typedef int bool;
  82.  
  83. #ifndef TRUE
  84. #define TRUE   1
  85. #endif
  86. #ifndef FALSE
  87. #define FALSE  0
  88. #endif
  89.  
  90. #define WIDTH MASPAR_XLEN
  91. #define HEIGHT MASPAR_YLEN
  92.  
  93. /* ------------------------- */
  94.  
  95. int running = FALSE;
  96.  
  97.  
  98.  
  99. void Xlights();
  100.  
  101. void visualise( info )
  102.  
  103.      char info[WIDTH][HEIGHT];
  104.  
  105. {
  106.   Xlights( info );
  107. }
  108.  
  109. void draw_lights();
  110. void setup();
  111. void redisplay();
  112.  
  113. char old_info[WIDTH][HEIGHT];
  114.  
  115. void init_info( info )
  116.  
  117. char info[WIDTH][HEIGHT];
  118.  
  119. {
  120.      int x;
  121.     int y;
  122.     XSetForeground(display, gc, white );
  123.     for( y = 0; y < HEIGHT; y++ )
  124.       for( x = 0; x < HEIGHT; x++ ) {
  125.         XFillRectangle( display, win, gc, panel_x(x)
  126.                ,panel_y(y),panel_width,panel_height);
  127.         info[x][y] = (char)5;
  128.       }
  129.     XFlush(display);
  130. }
  131.  
  132. void rand_info( info )
  133.  
  134. char info[WIDTH][HEIGHT];
  135.  
  136. {
  137. }
  138.  
  139. void print_info( info )
  140.  
  141. char info[WIDTH][HEIGHT];
  142.  
  143. {
  144.      int x;
  145.     int y;
  146.     for( y = 0; y < HEIGHT; y++ )
  147.     {
  148.       for( x = 0; x < WIDTH; x++ )
  149.         printf(" %d",(int)info[x][y]);
  150.       printf("\n");
  151.     }
  152. }
  153.  
  154. void Xlights( info)
  155. char info[WIDTH][HEIGHT];
  156. {
  157.   if (!(running)) {
  158.     setup(NULL);
  159.     init_info( old_info);
  160.   }
  161.   redisplay(info);
  162. }
  163.  
  164. void
  165. setup( display_name )
  166. char *    display_name;
  167. {
  168.     XSizeHints     hints;
  169.     int         i;
  170.     int         maxw = 0;
  171.     char *        maxname;
  172.     XColor         c;
  173.     Colormap     cmap;
  174.     int         selfpos = 0;
  175.     int        args_start;
  176.  
  177.     if ((display = XOpenDisplay( display_name )) == NULL )
  178.     {
  179.         IOdebug("failed to open display %s", XDisplayName( display_name ));
  180.         
  181.         exit(1);
  182.     }
  183.  
  184.     font = XLoadFont(display, "6x13");
  185.     fontinfo = XQueryFont(display, font);
  186.  
  187.     border = 1;
  188.  
  189.     light_height = 4;
  190.     light_width = light_height;
  191.     panel_width =  (light_width+2*border);
  192.     panel_height = (light_height+2*border);
  193.  
  194.     loadlight_x = border;
  195.     loadlight_y = border;
  196.     
  197.     screen = DefaultScreen(display);
  198.     visual = DefaultVisual(display,screen);
  199.     cmap = DefaultColormap(display, screen);
  200.     
  201.     width = (panel_width+2*border)*WIDTH+(2*border);
  202.     height = (panel_height+2*border)*HEIGHT+(2*border);
  203.     x = DisplayWidth(display,screen)-width-2*border;
  204.     y = DisplayHeight(display,screen)-height-2*border;
  205.     black = BlackPixel(display,screen);
  206.     white = WhitePixel(display,screen);
  207.  
  208.     if( visual->class == GrayScale )
  209.     {
  210.         colnames = greynames;
  211.         ncolors = sizeof(greynames)/4;
  212.     }
  213.     else
  214.     {
  215.         colnames = huenames;
  216.         ncolors = sizeof(huenames)/4;        
  217.     }
  218.  
  219.     XParseColor(display, cmap, "darkslategrey", &c );
  220.     XAllocColor(display, cmap, &c );
  221.     grey = c.pixel;
  222.  
  223.     for( i = 0; i < ncolors; i++ )
  224.     {
  225.         XParseColor(display, cmap, colnames[i], &c );
  226.         XAllocColor(display, cmap, &c );
  227.         colors[i] = c.pixel;
  228.     }
  229.     
  230.     win = XCreateSimpleWindow(display, RootWindow(display,screen),
  231.         x,y,width,height,border,white,grey);
  232.         
  233.  
  234.     icon_pixmap = XCreateBitmapFromData(display, win, icon_bits,
  235.         icon_width, icon_height);
  236.         
  237.     hints.flags = 0; /* selfpos?(USPosition|USSize):(PPosition|PSize); */
  238.     hints.x = x;
  239.     hints.y = y;
  240.     hints.width = width;
  241.     hints.height = height;
  242.     
  243.     XSetStandardProperties(display, win, WNAME, INAME, icon_pixmap,
  244.         NULL, 0, &hints );
  245.     
  246.     gc = XCreateGC(display, win, NULL, 0 );
  247.  
  248.     XSetLineAttributes(display, gc, 0, LineSolid, CapButt, JoinMiter );
  249.         
  250.     XSelectInput( display, win, 
  251.         StructureNotifyMask | ExposureMask | KeyPressMask );
  252.  
  253.     XMapWindow(display, win);
  254.  
  255. /*    running = TRUE;*/
  256. }
  257.  
  258. void redisplay( info)
  259. char info[WIDTH][HEIGHT];
  260. {
  261.   if( !running || XPending(display) )
  262.     {
  263.       XNextEvent( display, &xevent);
  264.       switch( xevent.type )
  265.     {
  266.     case Expose:
  267.     expose:
  268.       while(XCheckTypedEvent(display,Expose,&xevent));
  269.       XClearArea(display, win, 0, 0, width, height, FALSE);
  270.       XSetForeground(display, gc, black);
  271.       for(x = 0; x < WIDTH; x++ ) {
  272.  
  273.         for(y = 0; y < HEIGHT; y++ )
  274.           XFillRectangle(display, win, gc,
  275.                  panel_x(x), panel_y(y),
  276.                  panel_width,panel_height);
  277.       }
  278.       break;
  279.       
  280.     case ConfigureNotify:
  281.       width = xevent.xconfigure.width;
  282.       height = xevent.xconfigure.height;
  283.       XKillPendingRequests(display);
  284.       break;
  285.       
  286.     default:
  287.       break;
  288.     }
  289.     }
  290.  
  291.   if (!running) {
  292.     
  293.     running = TRUE;
  294.     XClearArea(display, win, 0, 0, width, height, FALSE);
  295.     XSetForeground(display, gc, black);
  296.     for(x = 0; x < WIDTH; x++ )  {
  297.       
  298.       for(y = 0; y < HEIGHT; y++ ) {
  299.  
  300.     XFillRectangle(display, win, gc,
  301.                panel_x(x), panel_y(y),
  302.                panel_width,panel_height);
  303.     old_info[x][y] = (char) 5;
  304.       }
  305.     }
  306.   }
  307.           
  308.   if( running ) {
  309.     
  310.     for( y = 0; y < HEIGHT; y++ ) {
  311.  
  312.       for( x = 0; x < WIDTH; x++ )
  313.  
  314.     if (info[x][y] != old_info[x][y]) {
  315.  
  316.       draw_lights((int)info[x][y],y,x);
  317.       old_info[x][y]=info[x][y];
  318.     }
  319.     }
  320.     XFlush(display);
  321.   }
  322. }
  323.  
  324.  
  325. void light( value, x, y )
  326.  
  327. int value;
  328. int x;
  329. int y;
  330.  
  331. {
  332.     int color = colors[ value ];
  333.     if (value == 0) color = black;
  334.  
  335.     XSetForeground( display, gc, color );
  336. #if 1
  337.     XFillRectangle(display, win, gc, x, y, light_width, light_height);
  338. #else
  339.     XFillArc( display, win, gc, x, y, light_width, light_height, 0, 360*64);
  340. #endif
  341. }
  342.  
  343. int 
  344. new_light( value)
  345. int value;
  346. {
  347.   if (value >= ncolors)
  348.     value = ncolors - 1;
  349.   return value;
  350. }
  351.  
  352. void draw_lights( value, x , y )
  353.  
  354. int value;
  355. int x;
  356. int y;
  357.  
  358. {
  359.   int new = new_light(value);
  360.   light(new, panel_x(x)+loadlight_x, panel_y(y)+loadlight_y);
  361. }
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.